﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Animation;
using Microsoft.Xna.Framework.Graphics;
using MapLibrary;
using Microsoft.Xna.Framework;

namespace MapLibrary
{
    class SpriteTypePanel
    {
        private const int OFFSET_Y = 10;
        private const int OFFSET_X = 25;
        private const int PADDING_X = 10;
        private const float ICON_WIDTH = 60.0f;
        private const float ICON_HEIGHT = 60.0f;

        private const int NUMBER_PER_PAGE = 8;

        private Texture2D mSelectTexture;

        private SpritePalette mSpritePalette;
        private ContentManager mContentManager;
        private List<SpriteType> mSpriteTypes;
        private int mCurrentSelection;
        private SpriteFont mFont;

        private SelectionManager<SpriteType> mSelectionManager;

        public SpriteTypePanel()
        {
            mSpriteTypes = new List<SpriteType>();
            mCurrentSelection = 0;
            mSelectionManager = new SelectionManager<SpriteType>(
                mSpriteTypes,
                NUMBER_PER_PAGE);
        }

        public void Load(ContentManager pContentManager)
        {
            mContentManager = pContentManager;

            mSelectTexture = pContentManager.Load<Texture2D>(@"./PalettePanel/palettePanelSelect");
            mFont = pContentManager.Load<SpriteFont>(@"./PalettePanel./PalettePanelFont");
        }

        public void LoadSpritePalette(SpritePalette pPalette)
        {
            mSpritePalette = pPalette;

            LoadSpriteTypes();
        }

        public string GetAssetNameForSpriteType(SpriteType pType)
        {
            int index = mSpriteTypes.IndexOf(pType);

            return mSpritePalette.SpriteTypeAssets[index];
        }

        public void Disable()
        {
            mSpriteTypes.Clear();
        }

        public void Draw(SpriteBatch pSpriteBatch)
        {
            int totalXOffset = OFFSET_X;
            int textureCount = 0;
            foreach (SpriteType type in mSelectionManager.CurrentPage)
            {
                pSpriteBatch.Draw(type.Icon, new Vector2(totalXOffset, OFFSET_Y), Color.White);
                if (textureCount == mSelectionManager.SelectionIndex)
                {
                    pSpriteBatch.Draw(
                        mSelectTexture,
                        new Vector2(totalXOffset, OFFSET_Y),
                        Color.White);
                }

                textureCount++;

                totalXOffset += (int)ICON_WIDTH + PADDING_X;
            }

            pSpriteBatch.DrawString(
                mFont,
                mSelectionManager.CurrentPageNumber + " (" + mSelectionManager.NumberOfPages + ")",
                new Vector2(OFFSET_X, 2 * OFFSET_Y + 60),
                Color.White);
        }

        public void MoveSelectionLeft()
        {
            mSelectionManager.MoveSelectionLeft();
        }

        public void MoveSelectionRight()
        {
            mSelectionManager.MoveSelectionRight();
        }

        public SpriteType GetSelectedSpriteType()
        {
            if (mCurrentSelection == 0)
            {
                if (mSpriteTypes.Count > 0)
                    return mSelectionManager.Selection;
                else
                    return null;
            }
            else
                return mSelectionManager.Selection;
        }

        private void LoadSpriteTypes()
        {
            foreach (string asset in mSpritePalette.SpriteTypeAssets)
            {
                mSpriteTypes.Add(mContentManager.Load<SpriteType>(asset));
            }
        }
    }
}
